package com.zjp;

import lombok.SneakyThrows;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.util.AddAliasesVisitor;
import net.sf.jsqlparser.util.SelectUtils;
import net.sf.jsqlparser.util.TablesNamesFinder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Set;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws JSQLParserException {
        System.out.println("Hello World!");

        testInsertParser();
    }


    public static void testInsertParser() throws JSQLParserException {

        String insertSql = "insert init test(c1,c2) values ('001','002')";

        Statement statement = CCJSqlParserUtil.parse(insertSql);

        if (statement instanceof Insert) {
            Insert insert = (Insert) statement;

            // 添加新列
            insert.addColumns(new Column("c3 "));
            ExpressionList<StringValue> expressions = (ExpressionList<StringValue>) insert.getValues().getExpressions();

            expressions.addExpression(new StringValue("003"));

            System.out.println(insert);
        }
    }


    public void test() throws JSQLParserException {


        String sqlStr = "select 1 from dual where a=b";

        PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(sqlStr);

        SelectItem selectItem =
                select.getSelectItems().get(0);
        Assertions.assertEquals(
                new LongValue(1)
                , selectItem.getExpression());

        Table table = (Table) select.getFromItem();
        Assertions.assertEquals("dual", table.getName());

        EqualsTo equalsTo = (EqualsTo) select.getWhere();
        Column a = (Column) equalsTo.getLeftExpression();
        Column b = (Column) equalsTo.getRightExpression();
        Assertions.assertEquals("a", a.getColumnName());
        Assertions.assertEquals("b", b.getColumnName());

    }

    @Test
    public void test2() throws JSQLParserException {


        // 从 SQL 中提取表名
        Statement statement = CCJSqlParserUtil.parse("SELECT * FROM MY_TABLE1");
        Select selectStatement = (Select) statement;
        TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
        Set<String> tableList = tablesNamesFinder.getTables(statement);
        System.out.println(tableList);
    }

    @Test
    public void test3() throws JSQLParserException {

        // 将别名应用于所有表达式
        Select select = (Select) CCJSqlParserUtil.parse("select a,b,c from test");
        final AddAliasesVisitor instance = new AddAliasesVisitor();
        instance.setPrefix("RT");
        select.getSelectBody().accept(instance, null);

        System.out.println(select);
    }

    @Test
    @SneakyThrows
    public void test4() {
        //将列或表达式添加到所选内容
        Select select = (Select) CCJSqlParserUtil.parse("select a from mytable");
        SelectUtils.addExpression(select, new Column("b"));

        System.out.println(select);
    }

}
